package com.triones.yjj.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.triones.yjj.entity.InviteRewardDO;
import com.triones.yjj.entity.OrderDO;
import com.triones.yjj.entity.ShareDO;
import com.triones.yjj.entity.vo.*;
import com.triones.yjj.mapper.InviteRewardMapper;
import com.triones.yjj.mapper.ShareMapper;
import com.triones.yjj.mapper.StaffMapper;
import com.triones.yjj.service.InviteRewardService;
import com.triones.yjj.service.ShareService;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分享相关 service 业务实现
 *
 * @Author: triones
 */
@Service
@RequiredArgsConstructor
public class InviteRewardServiceImpl implements InviteRewardService {

    private final InviteRewardMapper inviteRewardMapper;

    private final StaffMapper staffMapper;
    /**
     *邀请奖励列表
     * @param activityId
     * @param pageQuery
     * @return
     */
    public TableDataInfo<WxInviteRewardVO> selectPageList(Long activityId, PageQuery pageQuery) {
        QueryWrapper<InviteRewardDO> wrapper = new QueryWrapper<>();
        // 默认排序 时间倒叙
        if (StringUtils.isBlank(pageQuery.getOrderByColumn()) || StringUtils.isBlank(pageQuery.getIsAsc())) {
            wrapper.lambda().orderByDesc(InviteRewardDO::getCreateTime);
        }
        wrapper.eq("S.`status`",1);
        wrapper.eq("s.activity_id", activityId);
        Page<WxInviteRewardVO> result = inviteRewardMapper.selectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    /**
     * 活动送出奖品价值
     * @param activityId
     * @param userId
     * @return
     */
    public Double getActivityRewardAmount(Long activityId, Long userId) {
        return inviteRewardMapper.getActivityRewardAmount(activityId, userId);
    }

    public void setInviteReward(Long activityId, Long userId) {



    }
    public R<T> staffVerificationReward(WxStaffDetailVO staffVO, String verificationCode){
        StaffVO staffVO1 = staffMapper.selectVoById(staffVO.getId());
        if (ObjectUtil.isEmpty(staffVO1)) {
            return R.fail("员工不存在");
        }
        if (!staffVO1.getIsVerification().equals(1)) {
            return R.fail("员工暂无核销权限");
        }
        InviteRewardDO inviteRewardDO = getInviteRewardByVerificationCode(verificationCode);
        if(ObjectUtil.isEmpty(inviteRewardDO)){
            return  R.fail("订单不存在");
        }
        if(inviteRewardDO.getStatus().intValue()!=1&&inviteRewardDO.getStatus().intValue()!=2){
            return  R.fail("该订单未达成");
        }
        if(inviteRewardDO.getVerificationStatus().intValue()==2){
            return  R.fail("该订单已核销");
        }
        UpdateWrapper<InviteRewardDO> wrapper = new UpdateWrapper<>();
        wrapper.set("verification_status",2);
        wrapper.set("verification_staff_id",staffVO.getId());
        wrapper.set("verification_time", DateUtil.getDateTimeString());
        wrapper.eq("id",inviteRewardDO.getId());
        inviteRewardMapper.update(inviteRewardDO,wrapper);

        return R.ok("核销成功");
    }

    public InviteRewardDO getInviteRewardByVerificationCode(String verificationCode){
        QueryWrapper<InviteRewardDO> wrapper = new QueryWrapper<>();
        wrapper.eq("verification_code",verificationCode);
        return inviteRewardMapper.selectOne(wrapper);
    }

    public R<T> memberVerificationReward(MemberDetailVO memberDetailVO, String verificationCode, Long staffId) {
        StaffVO staffVO = staffMapper.selectVoById(staffId);
        if (ObjectUtil.isEmpty(staffVO)) {
            return R.fail("员工不存在");
        }
        if (!staffVO.getIsVerification().equals(1)) {
            return R.fail("员工暂无核销权限");
        }
        InviteRewardDO inviteRewardDO = getInviteRewardByVerificationCode(verificationCode);
        if (ObjectUtil.isEmpty(inviteRewardDO)) {
            return R.fail("订单不存在");
        }
        if (!inviteRewardDO.getMemberId().equals(memberDetailVO.getId())) {
            return R.fail("不能核销他人订单");
        }
        if(inviteRewardDO.getVerificationStatus().intValue()==2){
            return  R.fail("该订单已核销");
        }
        if(inviteRewardDO.getStatus().intValue()!=1&&inviteRewardDO.getStatus().intValue()!=2){
            return  R.fail("该订单未达成");
        }
        UpdateWrapper<InviteRewardDO> wrapper = new UpdateWrapper<>();
        wrapper.set("verification_status",2);
        wrapper.set("verification_staff_id",staffVO.getId());
        wrapper.set("verification_time", DateUtil.getDateTimeString());
        wrapper.eq("id",inviteRewardDO.getId());
        inviteRewardMapper.update(inviteRewardDO,wrapper);
        return R.ok("核销成功");
    }

    public WxMemberInviteRewardVO getMemberRewardInfoByCode(String verificationCode){
        return inviteRewardMapper.getMemberRewardInfoByCode(verificationCode);
    }

    public List<WxMemberInviteRewardVO> getMemberRewardList(Long memberId,Long userId){
        return inviteRewardMapper.getMemberRewardList(memberId,userId);
    }
}
